#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+10;
int T;
int n;
int a[N];
int red,blue;
int ans;
void dfs(int k,int sum){
    if(k==n+1){
        ans=max(ans,sum);
        return ;
    }
    if(red==a[k]){
        dfs(k+1,sum+a[k]);
    }
    else{
        int t=red;
        red=a[k];
        dfs(k+1,sum);
        red=t;
    }
    if(blue==a[k]){
        dfs(k+1,sum+a[k]);
    }
    else{
        int t=blue;
        blue=a[k];
        dfs(k+1,sum);
        blue=t;
    }
}
int dp[N];
int sum[N];
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>T;
    while(T--){
        red=blue=0;
        ans=0;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        if(n<=15){
            dfs(1,0);
            cout<<ans<<"\n";
        }
        else if(n<=20000){
            for(int i=1;i<=n;i++){
                if(a[i]==a[i-1]) sum[i]=sum[i-1]+a[i];
                else sum[i]=sum[i-1];
            }
            for(int i=1;i<=n;i++){
                dp[i]=sum[i];
                for(int j=1;j<i;j++){
                    if(a[i]==a[j]){
                        dp[i]=max(dp[i],dp[j]+sum[i]-sum[j]+a[i]);
                    }
                    else{
                        dp[i]=max(dp[i],dp[j]+sum[i]-sum[j]);
                    }
                }
            }
            cout<<dp[n]<<"\n";
        }
        else cout<<0<<"\n";
    }
    return 0;
}
